import 'dart:convert';

import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';
import 'package:http/http.dart' as http;


 import 'package:image_picker_web/image_picker_web.dart';


class ImageUploader {
   
  static Future uploadImage() async {
      final mediaData = await ImagePickerWeb.getImageAsBytes();
      if (mediaData == null) return;

      final uri =
          Uri.parse(Get.find<MyMethod>().myget('upIMG') + '/bbs/upload');
      final request = http.MultipartRequest('POST', uri)
        ..files.add(http.MultipartFile.fromBytes(
          'file',
          mediaData,
          filename: 'upload.jpg',
        ));
      request.headers.addAll({
        'Authorization': Get.find<MyMethod>().myget('token') ?? '',
      });

      Get.find<HTTPS>().loading();
      final response = await request.send();
      final responseData = await http.Response.fromStream(response);
      if (responseData.statusCode == 200) {
        final jsonData = jsonDecode(responseData.body);
        return jsonData['data']['url'];
      } else {
        print('请求失败: ${responseData.statusCode}');
      }
  }
}
